import 'dart:collection';

import 'package:flutter/material.dart';
import 'MyChatPage.dart';


 class MyStatefulWidget extends StatefulWidget {
  const MyStatefulWidget({Key? key}) : super(key: key);
  List<HashMap> _test(){

    List<HashMap> list = <HashMap>[];
    var accounts1 = new HashMap();
    var accounts2 = new HashMap();
    var accounts3 = new HashMap();
    var accounts4 = new HashMap();
    var accounts5 = new HashMap();

    accounts1['avatar']='https://img1.baidu.com/it/u=3490379535,534252992&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1693328400&t=52aa037d1b73d6187140c6472ca7e7af';
    accounts1['name']='李信';

    accounts1['content']='异乡人，怎能容忍故土脱离自己的手掌';

    accounts1['time']="8月15日";

    accounts1['doNotDisturb']=true;
    accounts1['unreadMessage']=99;

    accounts2['avatar']='https://img0.baidu.com/it/u=3069770223,3386636925&fm=253&fmt=auto&app=138&f=JPEG?w=400&h=400';
    accounts2['name']='张三';

    accounts2['content']='能提起沉重剑刃的手，握不住飞舞飘落的花';

    accounts2['time']="23:30";

    accounts2['doNotDisturb']=true;
    accounts2['unreadMessage']=1;

    accounts3['avatar']='https://img2.baidu.com/it/u=1783788654,21568302&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1693328400&t=b62cefa6f2ab37d358895aa10e9b1d70';
    accounts3['name']='张飞';

    accounts3['content']='屠魔的少年终究成魔，存活最后的魔做了救世主';

    accounts3['time']="12月15日";

    accounts3['doNotDisturb']=false;
    accounts3['unreadMessage']=105;


    accounts4['avatar']='https://img0.baidu.com/it/u=2267365228,2982031847&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1693328400&t=33c75169067673cbcf61262413c4190b';
    accounts4['name']='程咬金';

    accounts4['content']='北方的天空已经下雪了？';

    accounts4['time']="刚刚";

    accounts4['doNotDisturb']=true;
    accounts4['unreadMessage']=10;

    accounts5['avatar']='https://img2.baidu.com/it/u=53196982,2655758606&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1693328400&t=cc8b31a26d503392eb4722574bed08b4';
    accounts5['name']='姜维';

    accounts5['content']='今天是个好天气，准备去哪里玩呢？';

    accounts5['time']="12月15日";
    accounts5['doNotDisturb']=false;
    accounts5['unreadMessage']=0;

    list.add(accounts1);
    list.add(accounts2);
    list.add(accounts3);
    list.add(accounts4);
    list.add(accounts5);

    list.add(accounts1);
    list.add(accounts2);
    list.add(accounts3);
    list.add(accounts4);
    list.add(accounts5);

    list.add(accounts1);
    list.add(accounts2);
    list.add(accounts3);
    list.add(accounts4);
    list.add(accounts5);

    list.add(accounts1);
    list.add(accounts2);
    list.add(accounts3);
    list.add(accounts4);
    list.add(accounts5);

    list.add(accounts1);
    list.add(accounts2);
    list.add(accounts3);
    list.add(accounts4);
    list.add(accounts5);


    return list;
  }
  @override
  State<MyStatefulWidget> createState() => _MyStatefulWidgetState(items: _test());
}

 class _MyStatefulWidgetState extends State<MyStatefulWidget> with RestorationMixin{

   late RestorableRouteFuture<int> _counterRoute;



  final List<HashMap> items;

  _MyStatefulWidgetState({
    required this.items
  });

  double _top = 0.0; //距顶部的偏移
  double _left = 0.0;//距左边的偏移


// [SliverAppBar]s are typically used in [CustomScrollView.slivers], which in
// turn can be placed in a [Scaffold.body].
  @override
  Widget build(BuildContext context) {
    final title = 'Long List';
    return  MediaQuery.removePadding(
      removeTop: true,
      context: context,
      child: new Scaffold(
        body: new ListView.builder(
          itemCount: items.length,
          itemBuilder: (context, index) {
            return GestureDetector(
              behavior: HitTestBehavior.opaque,
              child: Column(
                children: <Widget>[
                  Flex(
                    direction: Axis.horizontal,
                    children: <Widget>[
                      Padding(padding: const EdgeInsets.all(10),
                        child: SizedBox(
                          height: 60.0,
                          child: Flex(
                            direction: Axis.vertical,
                            children: [
                              Stack(
                                alignment: FractionalOffset(1.2, -  0.16),
                                children: <Widget>[
                                  Container(
                                    height: 60.0,
                                    width: 60.0,
                                    // color: Colors.blue[50],
                                    child: Align(
                                      alignment: FractionalOffset(0.2, 0.6),
                                      child: Container(
                                        height: 300.0, //容器高度
                                        width: 300.0, //容器宽度
                                        decoration: BoxDecoration(
                                          //装饰线
                                            color: Colors.blue[600], //颜色
                                            // border: Border.all(
                                            //   //边框
                                            //   //color: Colors.deepOrange,
                                            //     width: 1.0),
                                            borderRadius: BorderRadius.all(
                                              //边框半径
                                              Radius.circular(10), //圆形
                                            ),
                                            image: DecorationImage(
                                              image: NetworkImage(
                                                '${items[index]['avatar']}',
                                              ),
                                              fit: BoxFit.cover,
                                              repeat: ImageRepeat.noRepeat, //平铺模式
                                            )),
                                      ),

                                      // Image.network(
                                      //   "${items[index]['avatar']}",
                                      //   width: 200,
                                      //   height: 200,
                                      // )

                                    ),
                                  ),
                                  Positioned(
                                    child: Container(
                                      height: 15.0, //容器高度
                                      // width: items[index]['unreadMessage'].toString().length >1?25: 15.0, //容器宽度
                                      width: items[index]['unreadMessage'].toString().length>2?25:items[index]['unreadMessage'] == 0? 0:items[index]['unreadMessage']<10?15:20,
                                      decoration: BoxDecoration(
                                        //装饰线
                                        color: Colors.red[600], //颜色
                                        // border: Border.all(
                                        //   //边框
                                        //   //color: Colors.deepOrange,
                                        //     width: 1.0),
                                        borderRadius: BorderRadius.all(
                                          //边框半径
                                          Radius.circular(20), //圆形
                                        ),
                                        // image: DecorationImage(
                                        //   image: NetworkImage(
                                        //     '${items[index]['avatar']}',
                                        //   ),
                                        //   fit: BoxFit.cover,
                                        //   repeat: ImageRepeat.noRepeat, //平铺模式
                                        // )
                                      ),
                                      child: Text("${items[index]['unreadMessage'].toString().length>2?"99+":items[index]['unreadMessage'] > 0? items[index]['unreadMessage']:""}",style: TextStyle(color: Colors.white ,),textAlign: TextAlign.center,),

                                    ),

                                  ),
                                ],
                              ),

                            ],
                          ),
                        ),
                      ),
                      Padding(padding: const EdgeInsets.all(0),),
                      Expanded(
                        flex: 8,
                        child: Container(
                            height: 60.0,
                            // color: Colors.blue,
                            child: new Column(
                                children: <Widget>[
                                  Flex(direction: Axis.horizontal,children: [
                                    Expanded(
                                      flex: 65,
                                      child: Container(
                                          height: 29.5,
                                          // color: Colors.green,
                                          child: Align(
                                            alignment: FractionalOffset(0.05, 0.8),
                                            child: Text("${items[index]['name']}",style: TextStyle(letterSpacing: 1,fontWeight: FontWeight.w500,fontSize: 20)),
                                          )

                                      ),
                                    ),

                                    Expanded(
                                      flex: 35,
                                      child: Container(
                                          height: 29.5,
                                          // color: Colors.green,
                                          child: Align(
                                            alignment: FractionalOffset(0.85, 0.8),
                                            // child: Text("11月17日",style: TextStyle(letterSpacing: 1,)),
                                            child: Text("${items[index]['time']}",style: TextStyle(letterSpacing: 1)),
                                          )

                                      ),
                                    ),
                                  ],),
                                  Flex(direction: Axis.horizontal,
                                    children: <Widget>[
                                      Expanded(
                                          flex: 65,
                                          child: Container(
                                            height: 29.5,
                                            width: 0,
                                            // color: Colors.blue[50],
                                            child: Align(
                                              alignment: FractionalOffset(0.05, 0.2),
                                              child: Text((items[index]['content']).toString().length< 12 ? "${items[index]['content']}":(items[index]['content']).toString().substring(0,12)+"...",
                                                style: TextStyle(letterSpacing: 1,color: Colors.grey,overflow: TextOverflow.ellipsis),
                                                maxLines: 3,overflow: TextOverflow.ellipsis,),
                                            ),
                                          )
                                      ),
                                      Expanded(
                                          flex: 35,
                                          child: Container(
                                            height: 29.5,
                                            // color: Colors.blue[50],
                                            child: Align(
                                              alignment: FractionalOffset(0.85, 0.2),
                                              child: items[index]['doNotDisturb']  == true ?Icon(Icons.notifications_off):  Container(),
                                            ),
                                          )
                                      ),

                                    ],),
                                  Expanded(
                                      flex: 2,
                                      child: Container(
                                        // height: 0.5,
                                        // color: Colors.blue[50],
                                        decoration: BoxDecoration(color: Colors.white, boxShadow: <BoxShadow>[
                                          BoxShadow(
                                            color: Colors.grey.shade900,
                                          ),
                                        ]),
                                        // child: Align(
                                        //   // alignment: FractionalOffset(0.05, 0.2),
                                        //   // child: Text("张三：我该睡觉了2",style: TextStyle(letterSpacing: 2,color: Colors.grey)),
                                        // ),
                                      )
                                  ),
                                ]
                            )
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            onTap: (){
              ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Row(children: <Widget>[
              // Icon(Icons.check,color: Colors.green,),
              Text('你点击了第${index}条数据')],), duration: Duration(milliseconds: 100),));
                _counterRoute.present("123");
                },

              onLongPressStart: (details) { // 一般的 onLongPress 没有位置信息，所以套了一个 GestureDetector
                showMenu(
                  context: context,
                  position: RelativeRect.fromLTRB(
                    details.globalPosition.dx,
                    details.globalPosition.dy,
                    details.globalPosition.dx,
                    details.globalPosition.dy,
                  ),
                  items: <PopupMenuEntry>[
                    new PopupMenuItem<String>(
                      value: '标记未读',
                      child: Row(children: <Widget>[
                        Padding( padding: EdgeInsets.fromLTRB(0.0, 0.0, 8.0, 0.0),),
                        Text('标记未读',style: TextStyle(color: Colors.black,),)
                      ]),
                      onTap: (){ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Row(children: <Widget>[
                        // Icon(Icons.check,color: Colors.green,),
                        Text('标记未读第${index}条数据')],), duration: Duration(milliseconds: 1),));},
                    ),
                    new PopupMenuItem<String>(
                      value: '置顶该聊天',
                      child: Row(children: <Widget>[
                        Padding( padding: EdgeInsets.fromLTRB(0.0, 0.0, 8.0, 0.0),),
                        Text('置顶该聊天',style: TextStyle(color: Colors.black,),)
                      ]),
                      onTap: (){ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Row(children: <Widget>[
                        // Icon(Icons.check,color: Colors.green,),
                        Text('置顶该聊天第${index}条数据')],), duration: Duration(milliseconds: 1),));},
                    ),
                    new PopupMenuItem<String>(
                      value: 'value01',
                      child: Row(children: <Widget>[
                        Padding( padding: EdgeInsets.fromLTRB(0.0, 0.0, 8.0, 0.0),),
                        Text('不显示该聊天',style: TextStyle(color: Colors.black,),)
                      ]),
                      onTap: (){ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Row(children: <Widget>[
                        // Icon(Icons.check,color: Colors.green,),
                        Text('不显示该聊天第${index}条数据')],), duration: Duration(milliseconds: 1),));},
                    ),
                    new PopupMenuItem<String>(
                      value: 'value01',
                      child: Row(children: <Widget>[
                        Padding( padding: EdgeInsets.fromLTRB(0.0, 0.0, 8.0, 0.0),),
                        Text('删除该聊天',style: TextStyle(color: Colors.black,),)
                      ]),
                      onTap: (){ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Row(children: <Widget>[
                        // Icon(Icons.check,color: Colors.green,),
                        Text('删除该聊天第${index}条数据')],), duration: Duration(milliseconds: 1),));},
                    ),
                  ],
                );
              },
            );
            // return new
          },
        ),
      ),
    );

  }
   @override
   String get restorationId => "";

   final RestorableInt _lastCount = RestorableInt(0);
   @override
   void initState() {
     super.initState();
     _counterRoute = RestorableRouteFuture<int>(
         onPresent: (NavigatorState navigator, Object? arguments) {
           // Defines what route should be shown (and how it should be added
           // to the navigator) when `RestorableRouteFuture.present` is called.
           return navigator.restorablePush(
             _counterRouteBuilder,
             arguments: arguments,
           );
         }, onComplete: (int count) {
       // Defines what should happen with the return value when the route
       // completes.
       setState(() {
         _lastCount.value = count;
       });
     });
   }
   @override
   void restoreState(RestorationBucket? oldBucket, bool initialRestore) {
     // Register the `RestorableRouteFuture` with the state restoration framework.
     registerForRestoration(_counterRoute, 'route');
     registerForRestoration(_lastCount, 'count');
   }

   @override
   void dispose() {
     super.dispose();
     _lastCount.dispose();
     _counterRoute.dispose();
   }



   // A static `RestorableRouteBuilder` that can re-create the route during
   // state restoration.
   static Route<int> _counterRouteBuilder(
       BuildContext context, Object? arguments) {
     return MaterialPageRoute<int>(
       builder: (BuildContext context) => MyChatPage(
         // title: arguments!.toString(),
       ),
     );
   }



 }
